﻿using System;
using System.Collections.Generic;
using System.Data.Objects.DataClasses;
using System.Linq;

namespace CivilRegistry.ControlledModel.Repository
{
    /// <summary>
    /// A repository for all cards.
    /// </summary>
    /// <typeparam name="TCard">The type of card.</typeparam>
    public abstract class CardRepository<TCard>: AbstractRepository<TCard>
        where TCard : EntityObject
    {
        /// <summary>
        /// To retrive the approved records.
        /// </summary>
        /// <param name="predicate">The predicate to filter.</param>
        /// <returns>List of cards.</returns>
        public List<TCard> GetApprovedRecords(Func<TCard, bool> predicate)
        {
            return this.FindAll(predicate).Where(tCard => ((dynamic)tCard).IsApproved == true).ToList<TCard>();
        }

        /// <summary>
        /// To retrive the non-approved cards.
        /// </summary>
        /// <param name="predicate">The predicate to filter.</param>
        /// <returns>List of cards.</returns>
        public List<TCard> GetNonApprovedRecords(Func<TCard, bool> predicate)
        {
            return this.FindAll(predicate).Where(tCard => ((dynamic)tCard).IsApproved == false).ToList<TCard>();
        }

        /// <summary>
        /// To retrive the deactive records.
        /// </summary>
        /// <param name="predicate">The predicate to filter.</param>
        /// <returns>List of cards.</returns>
        public List<TCard> GetDeactiveRecords(Func<TCard, bool> predicate)
        {
            return this.FindAll(predicate).Where(tCard => ((dynamic)tCard).DeactivateReason != null).ToList<TCard>();
        }

        /// <summary>
        /// To retrive the records by passed conditions.
        /// </summary>
        /// <param name="approved">The approved flag.</param>
        /// <param name="deactivated">The deactivated flag.</param>
        /// <param name="predicate">The predicate to filter.</param>
        /// <returns>List of cards.</returns>
        public List<TCard> GetRecordsBy(bool approved, bool deactivated, Func<TCard, bool> predicate)
        {
            if (approved && deactivated)
            {
                return this.FindAll(predicate).Where(tCard => ((dynamic)tCard).IsApproved == true && ((dynamic)tCard).DeactivateReason != null).ToList<TCard>();
            }

            if (approved)
            {
                return this.FindAll(predicate).Where(tCard => ((dynamic)tCard).IsApproved == true).ToList<TCard>();
            }

            if (deactivated)
            {
                return this.FindAll(predicate).Where(tCard => ((dynamic)tCard).DeactivateReason != null).ToList<TCard>();
            }

            return this.FindAll(predicate);
        }
    }
}
